package com.xbq.exceleditor.utils

import android.widget.TextView
import androidx.annotation.StringRes
import com.blankj.utilcode.util.ToastUtils
import com.xbq.exceleditor.R
import java.math.BigDecimal
import java.sql.Timestamp
import java.text.SimpleDateFormat
import java.util.regex.Pattern

fun TextView.value() = this.text.trim().toString()
fun TextView.intValue() =  Integer.valueOf(this.text.trim().toString())
fun TextView.decimalValue() =  BigDecimal(this.text.trim().toString())
fun TextView.timeValue() :Timestamp? {
    val value = this.value()
    if (value.isEmpty()) {
        return null
    }
    return SimpleDateFormat("yyyy-MM-dd HH:mm").parse(value)?.run {Timestamp(this.time)}
}



fun validatePhoneNumber(phone: String): Boolean {
    if (validateEmpty(phone, R.string.pls_type_phone_number)) {
        val patter = Pattern.compile("^1[0-9]{10}\$")
        val isphone = patter.matcher(phone).find()
        if (!isphone) {
            ToastUtils.showShort(R.string.pls_type_correct_phone_number)
        }
        return isphone
    }
    return false
}

fun validateEmpty(item: String?, @StringRes idRes: Int): Boolean {
    if (item?.isEmpty()?:false) {
        ToastUtils.showShort(idRes)
        return false
    }
    return true
}

fun validateSame(item: String, item2: String, @StringRes idRes: Int): Boolean {
    val isSame = item.equals(item2)
    if (!isSame) {
        ToastUtils.showShort(idRes)
    }
    return isSame
}
